/*
 *  Copyright (c) M2TK Project. All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package m2tk.assistant.common.ui;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionListener;

public class RoundPanel extends JPanel
{
    public RoundPanel()
    {
        putClientProperty("FlatLaf.style",
                          """
                          arc: 25; opaque: true; background: #232227;
                          """);
    }

    protected JSeparator separator()
    {
        JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);
        separator.putClientProperty("FlatLaf.style",
                                    """
                                    foreground: #18181A; stripeWidth: 2;
                                    """);
        return separator;
    }

    protected <T> JList<T> createList()
    {
        JList<T> list = new JList<>();
        list.putClientProperty("FlatLaf.style",
                               """
                               background: #232227;
                               """);
        return list;
    }

    protected JTree createTree()
    {
        JTree tree = new JTree();
        tree.putClientProperty("FlatLaf.style",
                               """
                               font: monospaced +1;
                               rowHeight: 30;
                               background: #232227;
                               """);
        return tree;
    }

    protected JTable createTable()
    {
        JTable table = new JTable();
        table.putClientProperty("FlatLaf.style",
                                """
                                font: monospaced +1;
                                rowHeight: 30;
                                cellMargins: 4,10,4,10;
                                background: #232227;
                                """);

        JTableHeader tableHeader = table.getTableHeader();
        tableHeader.putClientProperty("FlatLaf.style",
                                      """
                                      background: #232227;
                                      separatorColor: #232227;
                                      bottomSeparatorColor: #232227;
                                      height: 30;
                                      cellMargins: 4,10,4,10;
                                      """);
        DefaultTableCellRenderer headerRenderer = (DefaultTableCellRenderer) tableHeader.getDefaultRenderer();
        headerRenderer.setHorizontalAlignment(SwingConstants.LEADING);

        return table;
    }

    protected JScrollPane createScrollPane(Component view)
    {
        JScrollPane scrollPane = new JScrollPane(view);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        return scrollPane;
    }

    protected JSplitPane createHorizontalSplitPane(Component left, Component right)
    {
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setOneTouchExpandable(true);
        splitPane.setLeftComponent(left);
        splitPane.setRightComponent(right);
        splitPane.putClientProperty("FlatLaf.style",
                                    """
                                    dividerSize: 15; background: #232227;
                                    """);
        return splitPane;
    }

    protected JSplitPane createVerticalSplitPane(Component top, Component bottom)
    {
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        splitPane.setOneTouchExpandable(true);
        splitPane.setTopComponent(top);
        splitPane.setBottomComponent(bottom);
        splitPane.putClientProperty("FlatLaf.style",
                                    """
                                    dividerSize: 15; background: #232227;
                                    """);
        return splitPane;
    }

    protected JButton createControlButton(Icon icon, ActionListener listener)
    {
        JButton button = new JButton();
        button.setIcon(icon);
        button.addActionListener(listener);
        button.putClientProperty("FlatLaf.style",
                                 """
                                 borderWidth: 0; focusWidth: 0; innerFocusWidth: 0; innerOutlineWidth: 0;
                                 background: null;
                                 """);
        return button;
    }
}
